﻿using System;
using System.Collections.Generic;
using TestOnlineProject.Models.DataAccess;
using TestOnlineProject.Models.Entities;

namespace TestOnlineProject.Models.BusinessLogic
{
    public class UserAnswerBLO
    {
        UserAnswerDAO userAnswerDAO = new UserAnswerDAO();
        QuestionBLO questionBLO = new QuestionBLO();
        AnswerBLO answerBLO = new AnswerBLO();

        public int Add(int userTestId, int questionId, int answerId, int matchId)
        {
            try
            {
                var userAnswer = new UserAnswer
                {
                    UserTestId = userTestId,
                    QuestionId = questionId,
                    AnswerId = answerId,
                    IsSelected = true
                };

                if (matchId != 0)
                {
                    userAnswer.MatchId = matchId;
                    userAnswer.IsSelected = true;
                }

                var result = userAnswerDAO.Insert(userAnswer);
                return result;
            }
            catch (Exception)
            {
                return 0;
            }
            

        }

        public UserAnswer Find(int userTestId, int questionId, int answerId)
        {
            try
            {
                var userAnswer = userAnswerDAO.SelectByUserTestIdQuestionIdAnswerId(userTestId, questionId, answerId);
                return userAnswer;
            }
            catch (Exception)
            {
                return null;
            }
            
        }
        public List<Question> GetQuestions(int userTestId)
        {
            try
            {
                var questions = new List<Question>();
                var userAnswers = userAnswerDAO.SelectByUserTestId(userTestId);
                int temp = 0;
                foreach (var userAnswer in userAnswers)
                {
                    if(userAnswer.QuestionId != temp)
                    {
                        questions.Add(questionBLO.Find(userAnswer.QuestionId));
                        temp = userAnswer.QuestionId;
                    }
                }
                return questions;
            }
            catch (Exception)
            {
                return null;
            }
        } 

        public List<AnswerModel> GetAnswers(int userTestId,  int questionId)
        {
            try
            {
                var answers = new List<AnswerModel>();
                var userAnswers = userAnswerDAO.SelectByUserTestIdQuestionId(userTestId, questionId);
                foreach (var userAnswer in userAnswers)
                {
                    var answer = answerBLO.Find(userAnswer.AnswerId);
                    var answerModel = new AnswerModel();
                    answerModel.AnswerId = answer.AnswerId;
                    answerModel.Checked = userAnswer.IsSelected;
                    answerModel.Content = answer.Content;
                    if (userAnswer.MatchId != null) answerModel.MatchId = (int) userAnswer.MatchId;
                    answers.Add(answerModel);
                }
                return answers;
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}